package com.ef.web.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.ef.constant.ConfirmMessage;
import com.ef.constant.SystemContant;
import com.ef.domain.Leads;
import com.ef.domain.Menu;
import com.ef.domain.User;
import com.ef.service.LeadsService;
import com.ef.service.MenuService;
import com.ef.service.TodoEventService;
import com.ef.service.UserService;
import com.ef.web.domain.LeftMenu;
import com.opensymphony.xwork2.ActionContext;

public class LoginAction extends BaseAction<User> {

	private static final long serialVersionUID = 1L;

	private UserService userService;

	private MenuService menuService;
	
	private TodoEventService todoEventService;

	protected final org.apache.commons.logging.Log log = org.apache.commons.logging.LogFactory
			.getLog(LoginAction.class);

	private String loginName;
	private String password;
	private String validate;
	private String message;

	public String login() {

		// super administrator
		if (loginName.trim().equals("administrator")
				&& password.equals("administrator")) {
			return "main2";
		}

		// cyg100103 ??
		// if(super.getSessionUser()!=null){
		// return LOGIN_SUCCESS;
		// }

//		String code = super.getRandImageCode();
//		if (code == null || code.equals("")) {
//			this.setMessage(ConfirmMessage.MSG_TIME_OUT);
//			return LOGIN;
//		}
//
//		if (!code.equals(validate)) {
//			this.setMessage(ConfirmMessage.MSG_CODE_ERROR);
//			return LOGIN;
//		}

		String realPassword = userService.getPasswordByLoginName(loginName);
		if (realPassword.equals("")) {
			this.setMessage(ConfirmMessage.MSG_USER_ERROR);
			return LOGIN;
		}
		if (password.equals(realPassword)) {
			ActionContext ctx = ActionContext.getContext();

			HttpServletRequest request = (HttpServletRequest) ctx
					.get(ServletActionContext.HTTP_REQUEST);
			User user = userService.getUserByLoginName(loginName);
			request.getSession().setAttribute(SystemContant.CURRENT_USER, user);
			super.setSessionUser(user);
			todoEventService.refreshEvent();
			leadsService.refreshDeferLeads();
			return LOGIN_SUCCESS;
		} else {
			this.setMessage(ConfirmMessage.MSG_PWD_ERROR);
		}

		return LOGIN;
	}
	
	private LeadsService leadsService;

	//TODO
	public String loginCallCenter() {
		ActionContext ctx = ActionContext.getContext();
		HttpServletRequest request = (HttpServletRequest) ctx
		.get(ServletActionContext.HTTP_REQUEST);
	    String caller=request.getParameter("caller");
        String call=request.getParameter("call");
	    if (caller==null||caller.trim().length()==0||call==null||call.trim().length()==0){
	    	return null;
	    }
	    User user = userService.getUserByCaller(caller);
	    if (user==null){
	    	return null;
	    }
		request.getSession().setAttribute(SystemContant.CURRENT_USER, user);
		super.setSessionUser(user);
		callCenterLeadsUrl="/customer/TM/call";
		List<Leads> leads=leadsService.findByTelephone(call.trim());
		if (leads!=null&&leads.size()>0){
			callCenterLeadsUrl=callCenterLeadsUrl+"?id="+leads.get(0).getId();
		};
		return "loginCallCenter";
	}
	private String callCenterLeadsUrl;
	public String logout() {
		super.clearSessionUser();
		return LOGIN;
	}

	public String left() {

		User curUser = super.getSessionUser();
		String strmenu = curUser.getRole().getMenu();
		String[] ids = strmenu.split(",");
		List<Menu> menus = new ArrayList<Menu>();
		for (String s : ids) {
			Menu m = menuService.findById(Long.valueOf(s));
			if (m != null) {
				menus.add(m);
			}
		}
		String treeData = "";
		for (Menu menu : menus) {
			treeData += "d.add(" + menu.getId() + ","
					+ replace(menu.getParent()) + ",'" + menu.getName() + "','"
					+ menu.getLink() + "','','" + menu.getTarget() + "');\n ";
		}
		this.getRequest().setAttribute("_roleMenus", treeData);
		return LEFT;
	}

	private String replace(Menu parent) {
		if (parent != null)
			return parent.getId().toString();
		else
			return "0";
	}

	private void makeMenu() {
		User curUser = super.getSessionUser();
		String menu = curUser.getRole().getMenu();
		String[] ids = menu.split(",");
		List<Menu> menus = new ArrayList<Menu>();
		for (String s : ids) {
			menus.add(menuService.findById(Long.valueOf(s)));
		}
		this.getRequest().setAttribute("_roleMenus", menus);

		List<LeftMenu> leftmenus = new ArrayList<LeftMenu>();
		for (Menu me : menus) {
			if (me.getParent() == null || me.getParent().getId() == null) {
				LeftMenu m = new LeftMenu();
				m.setFirstMenu(me);
			} else {

			}

		}
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public String getLoginName() {
		return loginName;
	}

	public void setLoginName(String loginName) {
		this.loginName = loginName;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getValidate() {
		return validate;
	}

	public void setValidate(String validate) {
		this.validate = validate;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public MenuService getMenuService() {
		return menuService;
	}

	public void setMenuService(MenuService menuService) {
		this.menuService = menuService;
	}

	public LeadsService getLeadsService() {
		return leadsService;
	}

	public void setLeadsService(LeadsService leadsService) {
		this.leadsService = leadsService;
	}

	public String getCallCenterLeadsUrl() {
		return callCenterLeadsUrl;
	}

	public void setCallCenterLeadsUrl(String callCenterLeadsUrl) {
		this.callCenterLeadsUrl = callCenterLeadsUrl;
	}

	public TodoEventService getTodoEventService() {
		return todoEventService;
	}

	public void setTodoEventService(TodoEventService todoEventService) {
		this.todoEventService = todoEventService;
	}

}
